package com.bigdatan.b2c.controller.glass;

import com.alibaba.fastjson.JSON;
import com.bigdatan.b2c.conf.OnlineConfig;
import com.bigdatan.b2c.controller.AbstractController;
import com.bigdatan.b2c.controller.admin.vo.DetailDescInfoBO;
import com.bigdatan.b2c.controller.glass.vo.GlassGoodsVo;
import com.bigdatan.b2c.controller.glass.vo.SDKInfo;
import com.bigdatan.b2c.entity.Goods;
import com.bigdatan.b2c.service.IGoodsService;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import constant.SystemCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import util.JsonResponse;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by shangdong on 17/5/13.
 */
@RestController
@RequestMapping("/api")
public class GlassController extends AbstractController {

//    public static final String SDK_PATH = "items/glasses/";


    @Resource
    private IGoodsService goodsService;

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getGlassByCategoryID")
    public JsonResponse<List<GlassGoodsVo>> getGlassByCategoryID(Integer categoryID,HttpServletRequest request){
        JsonResponse<List<GlassGoodsVo>> res = new JsonResponse<>();
        Goods goods = new Goods();
        goods.setCategoryId(categoryID);
        List<Goods> pageFrontByGoodsCategory = goodsService.getPageFrontByGoodsCategory(goods);
        if (!CollectionUtils.isEmpty(pageFrontByGoodsCategory)){
            List<GlassGoodsVo> glassGoodsVos =  Lists.transform(pageFrontByGoodsCategory, goods1 -> {
                GlassGoodsVo g = getGlassGoodsVo(goods1);
                return g;
            });
            res.setRes(SystemCode.SUCCESS);
            res.setObj(glassGoodsVos);
            return res;
        }
        return res;
    }

    private GlassGoodsVo getGlassGoodsVo(Goods goods1) {
        GlassGoodsVo g = new GlassGoodsVo();
        BeanUtils.copyProperties(goods1, g, "imageDetail", "sdkInfo","detailDescribe");
        DetailDescInfoBO detailDescInfoBO = null;
        try{
            detailDescInfoBO = JSON.parseObject(goods1.getDetailDescribe(), DetailDescInfoBO.class);
        }catch (Exception e){
        }
        boolean notParseJson = detailDescInfoBO == null || StringUtils.isBlank(detailDescInfoBO.getDetailDescribe());
        g.setDetailDescribe(notParseJson ? goods1.getDetailDescribe() : detailDescInfoBO.getDetailDescribe());
        Iterable<String> imagesList = Splitter.on(',').trimResults().omitEmptyStrings().split(goods1.getImageDetail());
        g.setImageDetailList(Lists.newArrayList(imagesList));

        if (StringUtils.isBlank(goods1.getSdkInfo())){
            SDKInfo sdkInfo = new SDKInfo();
            sdkInfo.setPath(OnlineConfig.OLD_SDK_PATH+"glasses"+goods1.getGoodsId());
            g.setSdkInfo(sdkInfo);
        }else {
            SDKInfo sdkInfo = null;
            try{
                sdkInfo = JSON.parseObject(goods1.getSdkInfo(), SDKInfo.class);
            }catch (Exception e){
            }
            if (sdkInfo != null || sdkInfo.getPath() != null){
                g.setSdkInfo(sdkInfo);
            }

        }
        return g;
    }

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/getGlassDetailInfo")
    public JsonResponse<GlassGoodsVo> getGlassDetailInfo(Integer glassID,HttpServletRequest request){
        JsonResponse<GlassGoodsVo> res = new JsonResponse<>();
        Goods goods = goodsService.selectByPrimaryKey(glassID);
        if (goods != null){
            res.setRes(SystemCode.SUCCESS);
            res.setObj(getGlassGoodsVo(goods));
            return res;
        }
        return res;
    }

}
